package com.gxan.web.servlet.pay.provide;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gxan.common.LayuiRepResult;
import com.gxan.entity.Dep;
import com.gxan.entity.PayList;
import com.gxan.service.SalaryManagementService;
import com.gxan.service.impl.SalaryManagementServiceimpl;
import com.sun.deploy.net.HttpResponse;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

//import static com.sun.javafx.fxml.expression.Expression.add;


@WebServlet("/pay/provide/*")
public class SalaryManagementServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取uri地址
        String uri = request.getRequestURI();
        // 判断 -- 走不同方法
        switch(uri){
            case "/pay/provide/payroll":
                findAll(request,response);
                break;
            case "/pay/provide/department":
                findDepartment(request,response);
                break;
            case "/pay/provide/addpayroll":
                addPayRoll(request,response);
                break;
            case "/pay/provide/del":
                delPayRoll(request,response);
                break;
            case "/pay/provide/edit":
                editPayRoll(request,response);
                break;
            case "/pay/provide/batchdel":
                batchdelPayROll(request,response);
                break;
            case "/pay/provide/deps":
                finddeps(request,response);
                break;
            case "/pay/provide/search":
                search(request,response);
                break;
            case "/pay/provide/register":
                register(request,response);
                break;
            default:
                break;
        }
    }

    SalaryManagementService salaryManagementService = new SalaryManagementServiceimpl();

    //查询所有
    public void findAll(HttpServletRequest request, HttpServletResponse response) throws IOException{

        Integer page = Integer.parseInt(request.getParameter("page"));
        Integer limit = Integer.parseInt(request.getParameter("limit"));
        LayuiRepResult result = salaryManagementService.findAll(page,limit);
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(JSONObject.toJSONString(result));
        response.getWriter().flush();
        response.getWriter().close();

    }
    //添加记录
    public void addPayRoll(HttpServletRequest request, HttpServletResponse response){
        //添加成功？返回json数据回去
        response.setContentType("application/json;charset=utf-8");
        try {

            String params = request.getReader().readLine();
            Dep dep = JSONObject.parseObject(params, Dep.class);


            LayuiRepResult result = salaryManagementService.addRoll(dep);

            response.getWriter().write(JSONObject.toJSONString(result));
            response.getWriter().flush();
            response.getWriter().close();


        } catch (IOException e) {
            throw new RuntimeException(e);
        }


        // 将json字符串转换为Java对象时，我们需要用第三方fastjson(alibaba)依赖
//        Emp emp = JSONObject.parseObject(params,Emp.class);
//        EmpService empService = new EmpServiceImpl();
//        int result = empService.updateEmp(emp);
//        if(result > 0){
//            resultMap.put("code",0);
//            resultMap.put("msg","修改成功！");
//            // 需要将Java对象转换为json字符串对象才能在网络中进行传输
//            response.getWriter().write(JSONObject.toJSONString(resultMap));

    }
    //删除记录
    public void delPayRoll(HttpServletRequest request, HttpServletResponse response){
        try {
            response.setContentType("application/json;charset=utf-8");

            String id = request.getParameter("dept_father");
            LayuiRepResult result = salaryManagementService.delRoll(id);
            response.getWriter().write(JSONObject.toJSONString(result));
            response.getWriter().flush();
            response.getWriter().close();

        } catch (IOException e) {
            throw new RuntimeException(e);
        }



    }
    public void editPayRoll(HttpServletRequest request, HttpServletResponse response){

         response.setContentType("application/json;charset=utf-8");

        try {

            String params = request.getReader().readLine();
            PayList payList = JSONObject.parseObject(params,PayList.class);
            LayuiRepResult result = salaryManagementService.editRoll(payList);

            response.getWriter().write(JSONObject.toJSONString(result));
            response.getWriter().flush();
            response.getWriter().close();

        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }

    public void batchdelPayROll(HttpServletRequest request, HttpServletResponse response){

        response.setContentType("application/json;charset=utf-8");

        try {

            String[] ids = request.getParameterValues("ids");

            LayuiRepResult result = salaryManagementService.batchdelRoll(ids);
            
            response.getWriter().write(JSONObject.toJSONString(result));
            response.getWriter().flush();
            response.getWriter().close();

        } catch (IOException e) {
            throw new RuntimeException(e);
        }




    }
    public void findDepartment(HttpServletRequest request, HttpServletResponse response){

        response.setContentType("application/json;charset=utf-8");



        try {

            LayuiRepResult result = salaryManagementService.findDepartment();

            response.getWriter().write(JSONObject.toJSONString(result));
            response.getWriter().flush();
            response.getWriter().close();

        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }

    public void finddeps(HttpServletRequest request, HttpServletResponse response) {
        response.setContentType("application/json;charset=utf-8");

        try {
            Integer page = Integer.parseInt(request.getParameter("page"));
            Integer limit = Integer.parseInt(request.getParameter("limit"));
            LayuiRepResult result = salaryManagementService.findAllDeps(page, limit);
            response.getWriter().write(JSONObject.toJSONString(result));
            response.getWriter().flush();
            response.getWriter().close();

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public void search(HttpServletRequest request, HttpServletResponse response){
        response.setContentType("application/json;charset=utf-8");


        try {
            String depsName = request.getParameter("dept_father");

            LayuiRepResult result = salaryManagementService.search(depsName);

            response.getWriter().write(JSONObject.toJSONString(result));
            response.getWriter().flush();
            response.getWriter().close();

        } catch (IOException e) {
            throw new RuntimeException(e);
        }




    }

    //登记
    public void register(HttpServletRequest request, HttpServletResponse response){

        response.setContentType("application/json;charset=utf-8");
        try {
            String dep = request.getParameter("dept_full_name");
            LayuiRepResult result = salaryManagementService.findAlregisterl(dep);
            response.getWriter().write(JSONObject.toJSONString(result));
            response.getWriter().flush();
            response.getWriter().close();

        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }
}



